package com.tinytoon.hunghoaqua;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

public class Mario {
	/**
	 * Mario status
	 */
	public static final int STANDING = 0;
	public static final int MOVE_LEFT = 1;
	public static final int MOVE_RIGHT = 2;
	public static final int JUMP_UP = 3;
	public static final int FALL_DOWN = 4;
	public static final int HIT_BALL = 5;
	public int mStatus;

	/**
	 * Mario direction
	 */
	public static final int DIRECTION_LEFT = 0;
	public static final int DIRECTION_RIGHT = 1;
	public int mDirection;

	/**
	 * Input type from player
	 */
	public static final int INPUT_MOVE_LEFT = 1;
	public static final int INPUT_MOVE_RIGHT = 2;
	public static final int INPUT_JUMP = 3;
	public static final int INPUT_STAND = 4;
	public static int mInputType = INPUT_STAND;

	/**
	 * Array of bitmap for moving left, right, stand and jump
	 */
	Bitmap animationLeft[];
	Bitmap animationRight[];
	Bitmap animationJumpRight;
	Bitmap animationJumpLeft;
	Bitmap animationStandLeft;
	Bitmap animationStandRight;

	/**
	 * Type of animation
	 */
	public int mType;
	public static final int TYPE_NORMAL = 0;
	public static final int TYPE_FINISH_ANIMATION = 1;
	public static final int TYPE_HIT_BALL = 2;

	/**
	 * Animation steps
	 */
	public int mAnimatioStep = 0;
	public static final int STEP_ANIMATION_START = 0;
	public static final int STEP_ANIMATION_INTERVAL = 5;

	public float mX;
	public float mY;
	public float mY_onGround;
	public float mSpeedX;
	public float mSpeedY;
	public Bitmap mBitmap;

	public int mFrame;
	public static final int MAX_FRAME = 15;
	public float mMovingDistance;
	public long STEPS_AT_HIGHEST_POINT;

	public Mario(Resources res) {

		animationJumpRight = GameRes.getBitmap(res, "jump right");
		animationJumpLeft = GameRes.getBitmap(res, "jump left");
		animationStandLeft = GameRes.getBitmap(res, "stand left");
		animationStandRight = GameRes.getBitmap(res, "stand right");
		animationLeft = GameRes.getBitmapsLeft(res);
		animationRight = GameRes.getBitmapsRight(res);
		mType = TYPE_NORMAL;

		mStatus = STANDING;
		mDirection = DIRECTION_RIGHT;
		mBitmap = animationStandRight;
		mSpeedX = 0;
		mSpeedY = 0;
	}

	public void handleInput(int input, int value) {
		switch (input) {
		case INPUT_MOVE_LEFT:
			mSpeedX = -1;
			mX = mMovingDistance * value;
			break;
		case INPUT_MOVE_RIGHT:
			mSpeedX = 1;
			mX = mMovingDistance * value;
			break;
		case INPUT_JUMP:
			// If Mario still jumping
			if (mSpeedY != 0)
				return;

			mSpeedY = -1;
			mY -= mBitmap.getHeight() / 2;
			STEPS_AT_HIGHEST_POINT = 0;
			break;
		case INPUT_STAND:
			mSpeedX = 0;
			// mSpeedY = 0;
			break;
		default:
			break;
		}
	}

	/**
	 * Stand, move left, right or jump
	 */
	public void animate() {
		checkBorder();
	}

	public void checkBorder() {
		// left
		if (mX <= 0) {
			mSpeedX = 0;
			mX = 0;
		}
		// right
		else if (mX + mBitmap.getWidth() >= Panel.mWidth) {
			mSpeedX = 0;
			mX = Panel.mWidth - mBitmap.getWidth();
		}
	}

	public void doDraw(Canvas canvas) {
		// If Mario moves left
		if (mSpeedX < 0) {
			mStatus = MOVE_LEFT;
			mDirection = DIRECTION_LEFT;
			mFrame++;
		}
		// If Mario moves right
		else if (mSpeedX > 0) {
			mStatus = MOVE_RIGHT;
			mDirection = DIRECTION_RIGHT;
			mFrame++;
		}

		// If Mario jumps up
		if (mSpeedY < 0) {
			mStatus = JUMP_UP;
		}
		// If Mario falls down
		else if (mSpeedY > 0) {
			mStatus = FALL_DOWN;
		}

		// If Mario does nothing (stand)
		if (mSpeedX == 0 && mSpeedY == 0 && mStatus != HIT_BALL) {
			mSpeedX = 0;
			mFrame = 0;
			mStatus = STANDING;
		}

		if (mFrame >= MAX_FRAME) {
			mFrame = 0;
		}

		if (mStatus == MOVE_LEFT) {
			mBitmap = animationLeft[mFrame];
			canvas.drawBitmap(mBitmap, mX, mY, null);
		} else if (mStatus == MOVE_RIGHT) {
			mBitmap = animationRight[mFrame];
			canvas.drawBitmap(mBitmap, mX, mY, null);
		} else if (mStatus == JUMP_UP) {
			if (mDirection == DIRECTION_RIGHT)
				mBitmap = animationJumpRight;
			else
				mBitmap = animationJumpLeft;
			canvas.drawBitmap(mBitmap, mX, mY, null);
			// When Mario jumps to highest point
			float y = Panel.mHeight - mBitmap.getHeight() * 2.1f;
			if (mY >= y)
				mY -= mBitmap.getHeight() / 20;
			else {
				mSpeedY = 1;
			}
		} else if (mStatus == FALL_DOWN) {
			if (mDirection == DIRECTION_RIGHT)
				mBitmap = animationJumpRight;
			else
				mBitmap = animationJumpLeft;
			canvas.drawBitmap(mBitmap, mX, mY, null);

			// when Mario on ground
			if (mY >= mY_onGround) {
				mSpeedY = 0;
				mY = mY_onGround;
				mStatus = STANDING;
			} else {
				// roi nhanh dan deu
				if (STEPS_AT_HIGHEST_POINT++ <= 3) {
					mY += mBitmap.getHeight() / 40;
				} else {
					mY += mBitmap.getHeight() / 20;
				}
			}
		} else if (mStatus == HIT_BALL) {
			canvas.drawBitmap(mBitmap, mX, mY, null);
		} else {
			if (mDirection == DIRECTION_LEFT)
				mBitmap = animationStandLeft;
			else
				mBitmap = animationStandRight;
			canvas.drawBitmap(mBitmap, mX, mY, null);
		}
	}

	public static boolean checkCollision(Rect A, Rect B) {
		// If any of the sides from A are outside of B
		if (A.bottom < B.top)
			return false;
		if (A.top > B.bottom)
			return false;
		if (A.right < B.left)
			return false;
		if (A.left > B.right)
			return false;
		// If none of the sides from A are outside B
		return true;
	}

	public Rect getRect() {
		int w = mBitmap.getWidth();
		int h = mBitmap.getHeight();
		int left = (int) mX;
		int top = (int) mY;
		int right = (int) (mX + w);
		int bottom = (int) (mY + h);

		left += w / 5;
		right -= w / 5;
		bottom -= h / 5;
		return new Rect(left, top, right, bottom);
	}

	public Rect getHeadRect() {
		int w = mBitmap.getWidth();
		int left = (int) mX;
		int top = (int) mY;
		int right = (int) (mX + w);
		int bottom = top + 1;

		left += w / 5;
		right -= w / 5;
		return new Rect(left, top, right, bottom);
	}

	public Bitmap blurBitmap() {
		Bitmap src = mBitmap.copy(mBitmap.getConfig(), true);
		Bitmap alpha = src.extractAlpha();
		BlurMaskFilter blurMaskFilter = new BlurMaskFilter(3,
				BlurMaskFilter.Blur.NORMAL);
		Paint paint = new Paint();
		paint.setMaskFilter(blurMaskFilter);
		paint.setColor(Color.RED);

		Canvas canvas = new Canvas(src);
		canvas.drawBitmap(alpha, 0, 0, paint);

		return src;
	}

}
